home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2004 April / Gamestar_61_2004-04_dvdb.iso / DVDStar / Editace / hltp.exe / {app} / Source Code / Half-Life Model Viewer / src / FileAssociation.cpp < prev    next >
C/C++ Source or Header  |  1999-05-04  |  6KB  |  282 lines

  1. //
  2. //                 Half-Life Model Viewer (c) 1999 by Mete Ciragan
  3. //
  4. // file:           FileAssociation.cpp
  5. // last modified:  May 04 1999, Mete Ciragan
  6. // copyright:      The programs and associated files contained in this
  7. //                 distribution were developed by Mete Ciragan. The programs
  8. //                 are not in the public domain, but they are freely
  9. //                 distributable without licensing fees. These programs are
  10. //                 provided without guarantee or warrantee expressed or
  11. //                 implied.
  12. //
  13. // version:        1.2
  14. //
  15. // email:          mete@swissquake.ch
  16. // web:            http://www.swissquake.ch/chumbalum-soft/
  17. //
  18. #include "FileAssociation.h"
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <mx/mx.h>
  23.  
  24.  
  25.  
  26. FileAssociation *g_FileAssociation = 0;
  27.  
  28.  
  29.  
  30. FileAssociation::FileAssociation ()
  31. : mxWindow (0, 100, 100, 400, 210, "File Associations", mxWindow::Dialog)
  32. {
  33.     cExtension = new mxChoice (this, 5, 5, 220, 22, IDC_EXTENSION);
  34.  
  35.     //new mxButton (this, 230, 5, 75, 22, "Add", IDC_ADD);
  36.     //new mxButton (this, 310, 5, 75, 22, "Remove", IDC_REMOVE);
  37.  
  38.     new mxGroupBox (this, 5, 30, 380, 115, "Assocations");
  39.     rbAction[0] = new mxRadioButton (this, 10, 50, 120, 22, "program", IDC_ACTION1, true);
  40.     rbAction[1] = new mxRadioButton (this, 10, 72, 120, 22, "associated program", IDC_ACTION2);
  41.     rbAction[2] = new mxRadioButton (this, 10, 94, 120, 22, "HLMV default", IDC_ACTION3);
  42.     rbAction[3] = new mxRadioButton (this, 10, 116, 120, 22, "none", IDC_ACTION4);
  43.     leProgram = new mxLineEdit (this, 130, 50, 220, 22, "", IDC_PROGRAM);
  44.     leProgram->setEnabled (false);
  45.     bChooseProgram = new mxButton (this, 352, 50, 22, 22, ">>", IDC_CHOOSEPROGRAM);
  46.     bChooseProgram->setEnabled (false);
  47.  
  48.     rbAction[0]->setChecked (false);
  49.     rbAction[1]->setChecked (true);
  50.  
  51.     new mxButton (this, 110, 155, 75, 22, "Ok", IDC_OK);
  52.     new mxButton (this, 215, 155, 75, 22, "Cancel", IDC_CANCEL);
  53.  
  54.     initAssociations ();
  55. }
  56.  
  57.  
  58.  
  59. FileAssociation::~FileAssociation ()
  60. {
  61. }
  62.  
  63.  
  64.  
  65. int
  66. FileAssociation::handleEvent (mxEvent *event)
  67. {
  68.     if (event->event != mxEvent::Action)
  69.         return 0;
  70.  
  71.     switch (event->action)
  72.     {
  73.     case IDC_EXTENSION:
  74.     {
  75.         int index = cExtension->getSelectedIndex ();
  76.         if (index >= 0)
  77.             setAssociation (index);
  78.     }
  79.     break;
  80.  
  81.     case IDC_ACTION1:
  82.     case IDC_ACTION2:
  83.     case IDC_ACTION3:
  84.     case IDC_ACTION4:
  85.     {
  86.         leProgram->setEnabled (rbAction[0]->isChecked ());
  87.         bChooseProgram->setEnabled (rbAction[0]->isChecked ());
  88.  
  89.         int index = cExtension->getSelectedIndex ();
  90.         if (index >= 0)
  91.             d_associations[index].association = event->action - IDC_ACTION1;
  92.         
  93.     }
  94.     break;
  95.  
  96.     case IDC_PROGRAM:
  97.     {
  98.         int index = cExtension->getSelectedIndex ();
  99.         if (index >= 0)
  100.             strcpy (d_associations[index].program, leProgram->getLabel ());
  101.     }
  102.     break;
  103.  
  104.     case IDC_CHOOSEPROGRAM:
  105.     {
  106.         const char *ptr = mxGetOpenFileName (this, 0, "*.exe");
  107.         if (ptr)
  108.         {
  109.             leProgram->setLabel (ptr);
  110.  
  111.             int index = cExtension->getSelectedIndex ();
  112.             if (index >= 0)
  113.                 strcpy (d_associations[index].program, leProgram->getLabel ());
  114.         }
  115.     }
  116.     break;
  117.  
  118.     case IDC_OK:
  119.         saveAssociations ();
  120.  
  121.     case IDC_CANCEL:
  122.         setVisible (false);
  123.         break;
  124.     }
  125.  
  126.     return 1;
  127. }
  128.  
  129.  
  130.  
  131. void
  132. FileAssociation::initAssociations ()
  133. {
  134.     int i;
  135.  
  136.     cExtension->removeAll ();
  137.  
  138.     for (i = 0; i < 16; i++)
  139.         d_associations[i].association = -1;
  140.  
  141.     char path[256];
  142.     strcpy (path, mx::getApplicationPath ());
  143.     strcat (path, "/hlmv.fa");
  144.     FILE *file = fopen (path, "rt");
  145.     if (!file)
  146.         return;
  147.  
  148.     i = 0;
  149.     char line[256];
  150.     while (i < 16 && fgets (line, 256, file))
  151.     {
  152.         int j = 0;
  153.         while (line[++j] != '\"');
  154.         line[j] = '\0';
  155.         strcpy (d_associations[i].extension, &line[1]);
  156.  
  157.         while (line[++j] != '\"');
  158.         int k = j + 1;
  159.         while (line[++j] != '\"');
  160.         line[j] = '\0';
  161.         strcpy (d_associations[i].program, &line[k]);
  162.  
  163.         d_associations[i].association = atoi (&line[++j]);
  164.  
  165.         cExtension->add (d_associations[i].extension);
  166.         ++i;
  167.     }
  168.  
  169.     fclose (file);
  170.  
  171.     setAssociation (0);
  172. }
  173.  
  174.  
  175.  
  176. void
  177. FileAssociation::setAssociation (int index)
  178. {
  179.     cExtension->select (index);
  180.     leProgram->setLabel (d_associations[index].program);
  181.  
  182.     for (int i = 0; i < 4; i++)
  183.         rbAction[i]->setChecked (i == d_associations[index].association);
  184.  
  185.     leProgram->setEnabled (d_associations[index].association == 0);
  186.     bChooseProgram->setEnabled (d_associations[index].association == 0);
  187.  
  188.     // TODO: check for valid associtaion
  189. #ifdef WIN32__
  190.     char path[256];
  191.  
  192.     strcpy (path, mx_gettemppath ());
  193.     strcat (path, "/hlmvtemp.");
  194.     strcat (path, d_associations[index].extension);
  195.  
  196.     FILE *file = fopen (path, "wb");
  197.     if (file)
  198.         fclose (file);
  199.  
  200.     int val = (int) ShellExecute ((HWND) getHandle (), "open", path, 0, 0, SW_HIDE);
  201.     char str[32];
  202.     sprintf (str, "%d", val);
  203.     setLabel (str);
  204.     rbAction[1]->setEnabled (val != 31);
  205. /*
  206.     WORD dw = 0;
  207.     HICON hIcon = ExtractAssociatedIcon ((HINSTANCE) GetWindowLong ((HWND) getHandle (), GWL_HINSTANCE), path, &dw);
  208.     SendMessage ((HWND) getHandle (), WM_SETICON, (WPARAM) ICON_SMALL, (LPARAM) hIcon);
  209.     char str[32];
  210.     sprintf (str, "%d", (int) hIcon);
  211.     setLabel (str);
  212. */
  213.     DeleteFile (path);
  214.  
  215.     //DestroyIcon (hIcon);
  216. #endif
  217.  
  218.     rbAction[2]->setEnabled (
  219.         !mx_strcasecmp (d_associations[index].extension, "mdl") ||
  220.         !mx_strcasecmp (d_associations[index].extension, "tga") ||
  221.         !mx_strcasecmp (d_associations[index].extension, "wav")
  222.     );
  223. }
  224.  
  225.  
  226.  
  227. void
  228. FileAssociation::saveAssociations ()
  229. {
  230.     char path[256];
  231.  
  232.     strcpy (path, mx::getApplicationPath ());
  233.     strcat (path, "/hlmv.fa");
  234.  
  235.     FILE *file = fopen (path, "wt");
  236.     if (!file)
  237.         return;
  238.  
  239.     for (int i = 0; i < 16; i++)
  240.     {
  241.         if (d_associations[i].association == -1)
  242.             break;
  243.  
  244.         fprintf (file, "\"%s\" \"%s\" %d\n",
  245.             d_associations[i].extension,
  246.             d_associations[i].program,
  247.             d_associations[i].association);
  248.     }
  249.  
  250.     fclose (file);
  251. }
  252.  
  253.  
  254.  
  255. int
  256. FileAssociation::getMode (char *extension)
  257. {
  258.     for (int i = 0; i < 16; i++)
  259.     {
  260.         //if (!strcmp (d_associations[i].extension, mx_strlower (extension)))
  261.         if (!strcmp (d_associations[i].extension, extension))
  262.             return d_associations[i].association;
  263.     }
  264.  
  265.     return -1;
  266. }
  267.  
  268.  
  269.  
  270. char *
  271. FileAssociation::getProgram (char *extension)
  272. {
  273.     for (int i = 0; i < 16; i++)
  274.     {
  275.         //if (!strcmp (d_associations[i].extension, mx_strlower (extension)))
  276.         if (!strcmp (d_associations[i].extension, extension))
  277.             return d_associations[i].program;
  278.     }
  279.  
  280.     return 0;
  281. }
  282.